package com.huluobo.a2303mdmdemo.goods.pages

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2303mdmdemo.demo.MyBanner
import com.huluobo.a2303mdmdemo.goods.GoodsIntent
import com.huluobo.a2303mdmdemo.goods.GoodsViewModel
import com.huluobo.a2303mdmdemo.goods.entity.GoodsEntity
import com.huluobo.a2303mdmdemo.goods.entity.GoodsEntityItem
import com.huluobo.lib_base.StateType
import com.huluobo.lib_base.UIState
import com.huluobo.lib_http.Const
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/9/3.
 */

@Composable
fun GoodsListPage(navHostController: NavHostController, vm: GoodsViewModel = hiltViewModel()) {
    val list = remember {
        SnapshotStateList<GoodsEntityItem>()
    }

    var isShowProgress by remember {
        mutableStateOf(false)
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnFailed -> {
                        isShowProgress = false
                        ToastUtils.showLong("获取商品列表失败:${it.msg}")
                        Log.e("TagA", "获取商品列表失败:${it.msg}")
                    }

                    UIState.OnLoading -> {
                        isShowProgress = true
                    }

                    is UIState.OnSuccess<*> -> {
                        if (it.type == StateType.DEFAULT) {
                            isShowProgress = false
                            list.clear()
                            list.addAll(it.data as GoodsEntity)
                        }

                        if (it.type == StateType.ADD_TO_CART) {
                            ToastUtils.showLong("添加购物车成功")
                        }
                    }
                }
            }
        }
        vm.sendIntent(GoodsIntent.GetAllGoods(0, 1, 10))
    })

    Column(
        modifier = Modifier
            .padding(10.dp)
    ) {

//        LazyColumn(content = {
//            item {
//                Button(modifier = Modifier.fillMaxWidth(), onClick = {
//                    navHostController.navigate(RoutePath.CART)
//                }) {
//                    Column {
//                        Text(text = "Banner")
//                        Text(text = "购物车")
//                    }
//                }
//            }
//            items(list) {
//                GoodsItem(entityItem = it, vm = vm)
//            }
//        }, verticalArrangement = Arrangement.spacedBy(10.dp))
//        val scrollState = rememberScrollState()
//        Column(modifier = Modifier.verticalScroll(scrollState)) {
//            Button(onClick = {}) {
//                Text(text = "购物车")
//            }
//            repeat(list.size) {
//                GoodsItem(entityItem = list[it], vm = vm)
//            }
//        }

//        Scaffold(
//            topBar = {
//                MyBanner()
//            }
//        ) {
//            LazyVerticalGrid(modifier = Modifier.padding(it), columns = GridCells.Fixed(2), content = {
//                items(list){
//                    GoodsItem(entityItem = it, vm = vm)
//                }
//            })
//        }

        LazyVerticalGrid(columns = GridCells.Fixed(2), content = {
            //item 解决首个item的布局
            //GridItemSpan 横跨2行
            item(span = { GridItemSpan(2) }) {
                MyBanner()
            }
            items(list) {
                GoodsItem(entityItem = it, vm = vm)
            }
        })
    }
}

@Composable
fun GoodsItem(entityItem: GoodsEntityItem, vm: GoodsViewModel) {
    Column(modifier = Modifier.padding(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
        AsyncImage(model = entityItem.goods_default_icon, contentDescription = "", modifier = Modifier.fillMaxWidth())
        Text(text = entityItem.goods_desc, maxLines = 1, overflow = TextOverflow.Clip)
        Text(text = "价格:${entityItem.goods_default_price}", color = Color.Red)
        Button(modifier = Modifier.fillMaxWidth(), onClick = {
            val map = mapOf(
                Const.PARAM_GOODS_ID to entityItem.id,
                Const.PARAM_COUNT to 1
            )
            vm.sendIntent(GoodsIntent.AddToCart(map))
        }) {
            Text(text = "添加购物车")
        }
    }
}